package com.wh.wisdomsite.xbox.server.controller.me;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.wh.wisdomsite.xbox.common.util.ResManager;
import com.wh.wisdomsite.xbox.me.entity.CraneRecord;
import com.wh.wisdomsite.xbox.me.service.ICraneRecordService;
import com.wh.wisdomsite.xbox.pojo.me.CraneRecordIn;
import com.wh.wisdomsite.xbox.pr.entity.Project;
import com.wh.wisdomsite.xbox.server.util.WrapperUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * 起重器械项目设备运行记录 控制层
 * @package com.wh.wisdomsite.xbox.server.controller.me
 * @author 蔡道行
 * @version V1.0
 * @date 15:05 2018/9/5
 * @Copyright 公司版权所有
 * @Company 贵州万华科技有限公司Copyright (c) 2014
 */

@Controller
@RequestMapping(value = "/craneRecord")
public class CraneRecordController {
    private static final Log logger = LogFactory.getLog(CraneRecordController.class);

    @Autowired
    private ICraneRecordService iCraneRecordService;

    /**
     * 跳转到起重机械运行数据
     * @return
     */
    @RequiresPermissions("rules:me_craneRecord_page")
    @RequestMapping(value = "/index.do", method = RequestMethod.GET)
    public String index(HttpServletRequest request ) {
        String prId = request.getParameter("prId");
        request.setAttribute("prId",prId);
        return "me/craneRecord/craneHistoricalDataList";
    }

    /**
     * 获取起重机械运行数据
     *
     * @param response
     * @param in
     */
    @RequiresPermissions("rules:me_craneRecord_find")
    @RequestMapping(value = "/craneHistoricalData.do")
    public void dustDevRecordList(HttpServletResponse response, CraneRecordIn in) {
        try {
            logger.info("加载起重机械运行数据列表-->>开始");
            if (in.getPage() == 0) in.setPage(1);
            if (in.getRows() == 0) in.setRows(10);
            Page<Project> page = new Page<Project>();
            page.setCurrent(in.getPage());
            page.setSize(in.getRows());
            //设置条件
            EntityWrapper<CraneRecord> ewCraneRecord = new EntityWrapper<CraneRecord>();
            ewCraneRecord.setEntity(new CraneRecord());
            if(StringUtils.isNotEmpty(in.getPrId())){
                ewCraneRecord.eq("p1.prId",in.getPrId());
            }else{
                //如果没有项目id，则不让数据查询出结果
                ewCraneRecord.eq("p1.prId",-1);
            }
            if(StringUtils.isNotEmpty(in.getDeviceId())){
                ewCraneRecord.eq("cp1.deviceId",in.getDeviceId());
            }
            if(StringUtils.isNotEmpty(in.getStartDate()) && StringUtils.isNotEmpty(in.getEndDate())){
                ewCraneRecord.between("cr.collectTime",in.getStartDate(),in.getEndDate()+" "+"23:59:59");
            }
            Page<Map<String, Object>> craneRecordPage = iCraneRecordService.getMapsPage(page, ewCraneRecord);
            JSONObject json = new JSONObject();
            JSONArray array = new JSONArray();
            if (craneRecordPage.getRecords().size() > 0) {
                for(Map<String, Object> record : craneRecordPage.getRecords()){
                    JSONObject object = new JSONObject(record);
                    if(record.get("craneProject") != null){
                        object.put("craneProject",new JSONObject(record.get("craneProject")));
                    }
                    if(record.get("project") != null){
                        object.put("project",new JSONObject(record.get("project")));
                    }
                    array.put(object);
                }
            }
            json.put("total", craneRecordPage.getTotal());
            json.put("rows", array);
            ResManager.getJsonOut(response).print(json);
            logger.info("加载起重机械运行数据列表-->>结束");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 起重机械运行数据详情
     * @param id
     * @param request
     * @return
     */
    @RequestMapping("craneHistoricalDataDesc.do")
    public String craneHistoricalDataDesc(String id, HttpServletRequest request) {
        logger.info("起重机械运行数据详情-->>开始");
        Map<String , Object> craneRecord = iCraneRecordService.getMapById(id);
        request.setAttribute("craneRecord", craneRecord);
        logger.info("起重机械运行数据详情-->>结束");
        return "me/craneRecord/craneHistoricalDataDesc";
    }

}

